import moment from 'moment';

export default {
    namespace: 'discuss',
    state: {
        data: [
            {
                id: 'd01',
                applyDate: '2017-01-15 12:12',
                applierId: 'applierId01',
                applierName: '张楚',
                departmentId: 'dep0001',
                departmentName: '骨科',
                institutionId: 'jg0001',
                institutionName: '高新区分院',
                purpose: '讨论患者的病情是否能够直接手术，讨论患者的病情是否能够直接手术！',
                doctorIds: ['dct01', 'dct02', 'dct03'],
                patientId: '000001',
                startTime: '2017-01-15 12:12',
                status: '0',
            }, {
                id: 'd02',
                applyDate: '2017-01-15 12:12',
                applierId: 'applierId01',
                applierName: '张楚',
                departmentId: 'dep0001',
                departmentName: '骨科',
                institutionId: 'jg0001',
                institutionName: '高新区分院',
                purpose: '讨论患者的病情是否能够直接手术，讨论患者的病情是否能够直接手术！',
                doctorIds: ['dct01', 'dct02', 'dct03'],
                patientId: '000002',
                startTime: '2017-01-15 12:12',
                status: '0',
            }, {
                id: 'd03',
                applyDate: '2017-01-15 12:12',
                applierId: 'applierId01',
                applierName: '张楚',
                departmentId: 'dep0001',
                departmentName: '骨科',
                institutionId: 'jg0001',
                institutionName: '高新区分院',
                purpose: '讨论患者的病情是否能够直接手术，讨论患者的病情是否能够直接手术！',
                doctorIds: ['dct01', 'dct02', 'dct03'],
                patientId: '000006',
                startTime: '2017-01-15 12:12',
                status: '0',
            },
        ],
        current: {
            id: null,
        },
        tempDiscuss: null,
    },
    reducers: {
        setState(state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },
    },
    effects: {
        *initTempDiscuss({ payload }, { put, select }) {
            const { tempDiscuss } = yield select((state) => state.discuss);
            if (tempDiscuss === null) {
                yield put({
                    type: 'setState',
                    payload: {
                        tempDiscuss: {
                            applierId: 'applierId03',
                            applierName: '张三',
                            departmentId: 'dep0002',
                            departmentName: '胸内科',
                            institutionId: 'jg0001',
                            institutionName: '高新区分院',
                            purpose: '',
                            doctorIds: [],
                            patientId: '',
                            startTime: moment(),
                        }
                    }
                });
            }
        },
        *init({ payload }, { put, select }) {
            const {
                current: { id: ucrId },
                data,
            } = yield select((state) => state.discuss);
            if (!ucrId) {
                if (data.length > 0) {
                    const { id } = data[0];
                    yield put({
                        type: 'setState',
                        payload: {
                            current: {
                                id,
                            }
                        },
                    });
                }
            }
        },
    },
    subscriptions: {
        setup(args) {
            const { dispatch, history } = args;
            history.listen(location => {
                if (location.pathname === '/home/discuss') {
                    dispatch({
                        type: 'init',
                    });
                }
                if (location.pathname === '/home/discuss/create') {
                    dispatch({
                        type: 'initTempDiscuss',
                    });
                }
            });
        }
    },
};
